--------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  /Users/pabloguzmanpinto/Dropbox/PAME 3ie/Do-files/Logs/4_Prepare variables 2010.log
  log type:  text
 opened on:  14 Dec 2015, 15:30:35

. 
. 
. ************************************************************************************************
> ***********     
. *** 1. Variable creation
. use "${path_data}/cuestionario2010_adm", clear

. 
. 
. // Consumer price index, depending of month of interview
.         gen ipc_nov2009=100.30

.         generat ipc= 101.87 if date_month==7
(13866 missing values generated)

.         replace ipc= 101.77 if date_month ==8
(8346 real changes made)

.         replace ipc= 102.18 if date_month ==9
(3874 real changes made)

.         replace ipc= 102.28 if date_month ==10
(309 real changes made)

. 
.                 
.         ** Labor income
.                 ** Labor income - primary occupation
.                         * Labor income - primary occupation - wage (monthly for wage earners acc
> ording to d05)
.                         quiet gen ya1_sueldo=0 if ind_c2010==1

.                         quiet replace ya1_sueldo= ya1_sueldo + d06m if (d06m!=. & d06m!=88 & d06
> m!=99)

.                         quiet summ d06m if (d06m<=5000 & d06m!=88 & d06m!=99) , de 

.                         quiet gen rest=1 if (d06m==. | d06m==88 | d06m==99)  /*restriction for t
> his variable*/          

.                         quiet replace ya1_sueldo =ya1_sueldo+ r(p50) if d06t==1 & rest==1 /*impu
> tation of median*/              

.                         quiet replace ya1_sueldo =ya1_sueldo+ 7500 if d06t==2 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo+ 12500 if d06t==3 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo+ 22500 if d06t==4 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo + 40000 if d06t==5 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo+ 75000 if d06t==6 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo+ 125000 if d06t==7 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo + 175000 if d06t==8 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo + 250000 if d06t==9 & rest==1

.                         quiet replace ya1_sueldo= ya1_sueldo + 350000 if d06t==10 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 450000 if d06t==11 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 550000 if d06t==12 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 650000 if d06t==13 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 750000 if d06t==14 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 850000 if d06t==15 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 950000 if d06t==16 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 1250000 if d06t==17 & rest==1

.                         quiet replace ya1_sueldo=ya1_sueldo+ 2250000 if d06t==18 & rest==1

.                         quiet summ d06m if (d06m>3000000 & d06m!=.), de 

.                         quiet replace ya1_sueldo=ya1_sueldo+  max(3000000,r(p50)) if d06t==19 & 
> rest==1 /*imputation of median or CLP 3000000*/

.                         replace ya1_sueldo=. if rest==1 & (d06t==88 | d06t==99)
(190 real changes made, 190 to missing)

.                         replace ya1_sueldo=. if (d05==3 | d05==4 | d05==6 | d05==7 | d05==9) & d
> 06m==. & d06t==. 
(0 real changes made)

.                         replace ya1_sueldo=0 if d05==8 & ya1_sueldo==.
(0 real changes made)

.                         drop rest 

.                         
.                         * Labor income - primary occupation - business (para indepentents and em
> ployers, variable d07)
.                         quiet gen ya1_negocio=0 if ind_c2010==1

.                         quiet replace ya1_negocio=ya1_negocio + d07m if (d07m!=. & d07m!=88 & d0
> 7m!=99)

.                         quiet summ d07m if (d07m<=5000 & d07m!=88 & d07m!=99) , de 

.                         quiet gen rest=1 if (d07m==. | d07m==88 | d07m==99)

.                         quiet replace ya1_negocio=ya1_negocio+ r(p50) if d07t==1 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 7500 if d07t==2 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 12500 if d07t==3 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 22500 if d07t==4 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 40000 if d07t==5 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 75000 if d07t==6 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 125000 if d07t==7 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 175000 if d07t==8 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 250000 if d07t==9 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio + 350000 if d07t==10 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 450000 if d07t==11 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 550000 if d07t==12 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 650000 if d07t==13 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 750000 if d07t==14 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 850000 if d07t==15 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 950000 if d07t==16 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 1250000 if d07t==17 & rest==1

.                         quiet replace ya1_negocio=ya1_negocio+ 2250000 if d07t==18 & rest==1

.                         quiet summ d07m if (d07m>3000000 & d07m!=.) , de 

.                         replace ya1_negocio =ya1_negocio+ max(3000000,r(p50)) if d07t==19 & rest
> ==1 /*imputation of median or CLP 3000000*/
(1 real change made)

.                         replace ya1_negocio=. if rest==1 & (d07t==88 | d07t==99)
(149 real changes made, 149 to missing)

.                         drop rest       

.                         replace ya1_negocio=. if (d05==1 | d05==2) & d07m==. & d07t==.
(0 real changes made)

.                         
.                                                 
.                         * Labor income - primary occupation - business products withdrew for per
> sonal expenses (for indepentents and employers variable d08)
.                         quiet gen ya1_productos=0 if ind_c2010==1

.                         quiet replace ya1_productos=ya1_productos + d08m if (d08m!=. & d08m!=88 
> & d08m!=99)

.                         quiet summ d08m if (d08m<=5000 & d08m!=88 & d08m!=99) , de 

.                         qui gen rest = 1 if (d08m==. | d08m==88 | d08m==99)

.                         quiet replace ya1_productos=ya1_productos + r(p50) if d08t==1 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 7500 if d08t==2 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 12500 if d08t==3 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 22500 if d08t==4 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 40000 if d08t==5 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 75000 if d08t==6 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 125000 if d08t==7 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 175000 if d08t==8 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 250000 if d08t==9 & rest==1

.                         quiet replace ya1_productos=ya1_productos + 350000 if d08t==10 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 450000 if d08t==11 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 550000 if d08t==12 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 650000 if d08t==13 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 750000 if d08t==14 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 850000 if d08t==15 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 950000 if d08t==16 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 1250000 if d08t==17 & rest==1

.                         quiet replace ya1_productos=ya1_productos+ 2250000 if d08t==18 & rest==1

.                         quiet summ d08m if (d08m>3000000 & d08m!=88 & d08m!=99) , de 

.                         quiet replace ya1_productos =ya1_productos + max(3000000,r(p50)) if d08t
> ==19 & rest==1

.                         replace ya1_productos=. if rest==1 & (d08t==88 | d08t==99)
(20 real changes made, 20 to missing)

.                         drop rest

. 
.                         
.                 ** Labor income - secondary occupation
.                         * Labor income - secondary occupation - wage (monthly for wage earners a
> ccording to d10)
.                         quiet gen ya2_sueldo=0 if ind_c2010==1  

.                         quiet replace ya2_sueldo= ya2_sueldo + d10m if (d10m!=. & d10m!=88 & d10
> m!=99)

.                         quiet summ d10m if (d10m<=5000 & d10m!=88 & d10m!=99) , de 

.                         qui gen rest = 1 if (d10m==. | d10m==88 | d10m==99)

.                         quiet replace ya2_sueldo =ya2_sueldo + r(p50) if d10t==1 & rest==1

.                         quiet replace ya2_sueldo =ya2_sueldo + 7500 if d10t==2 & rest==1

.                         quiet replace ya2_sueldo =ya2_sueldo + 12500 if d10t==3 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo + 22500 if d10t==4 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo + 40000 if d10t==5 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo + 75000 if d10t==6 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo + 125000 if d10t==7 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo + 175000 if d10t==8 & rest==1

.                         quiet replace ya2_sueldo= ya2_sueldo + 250000 if d10t==9 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo + 350000 if d10t==10 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 450000 if d10t==11 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 550000 if d10t==12 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 650000 if d10t==13 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 750000 if d10t==14 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 850000 if d10t==15 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 950000 if d10t==16 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 1250000 if d10t==17 & rest==1

.                         quiet replace ya2_sueldo=ya2_sueldo+ 2250000 if d10t==18 & rest==1

.                         quiet summ d10m if (d10m>3000000 & d10m!=88 & d10m!=99) , de 

.                         replace ya2_sueldo =ya2_sueldo + max(3000000,r(p50)) if d10t==19 & rest=
> =1 /*imputation of median or CLP 3000000*/
(0 real changes made)

.                         replace ya2_sueldo=. if rest==1 & (d10t==88 | d10t==99)
(31 real changes made, 31 to missing)

.                         drop rest

.                         replace ya2_sueldo=. if (d09==1 | d09==2 | d09==3) & d10m==. & d10t==.
(0 real changes made)

.                         
.                         
.                 ** Imputation of missing values for observations with no information on work var
> iables (d01, d02, d03)
.                 foreach x in ya1_sueldo ya1_negocio ya1_productos ya2_sueldo {
  2.                         replace `x' = . if ((d01!=1 & d01!=2) & (d02!=1 & d02!=2) & (d03!=1 &
>  d03!=2)) ///
>                                                                 | ((d01==2) & (d02!=1 & d02!=2) 
> & (d03!=1 & d03!=2)) ///
>                                                                 | ((d01==2) & (d02==2) & (d03!=1
>  & d03!=2)) 
  3.                 }                                       
(5126 real changes made, 5126 to missing)
(5126 real changes made, 5126 to missing)
(5126 real changes made, 5126 to missing)
(5126 real changes made, 5126 to missing)

.                         
. 
.                 ** Grouping of income coming from primary and secondary occupation
.                 egen ya1=  rsum(ya1_sueldo ya1_negocio ya1_productos) if ya1_sueldo!=. | ya1_neg
> ocio!=. | ya1_productos!=.
(6463 missing values generated)

.                 gen ya2 = ya2_sueldo
(6494 missing values generated)

.                 egen ylaborales = rsum(ya1 ya2) if ya1!=. | ya2!=.
(6463 missing values generated)

.                 replace ylaborales=. if ya1_negocio==. &  ya1_productos==. & (d09==4 | d09==88 |
>  d09==88)
(12 real changes made, 12 to missing)

.                 replace ylaborales=. if ya1_negocio==. &  ya1_productos==. & ya2_sueldo==.
(0 real changes made)

.                 replace ylaborales=. if ya1_sueldo==. & (d09==4 | d09==88 | d09==88)
(184 real changes made, 184 to missing)

.                 replace ylaborales=. if ya1_sueldo==. & ya2_sueldo==.
(3 real changes made, 3 to missing)

. 
.                 
.                 ** Income coming from dependent and independent activities
.                 gen ydeptes= ya1_sueldo
(6653 missing values generated)

.                 replace ydeptes = ydeptes + ya2_sueldo if d09==3 & ya2_sueldo!=. 
(51 real changes made)

.                 
.                 egen yindeptes= rsum(ya1_negocio ya1_productos) if ya1_negocio!=. | ya1_producto
> s!=.
(6475 missing values generated)

.                 replace yindeptes = yindeptes + ya2_sueldo if (d09==1 | d09==2) & ya2_sueldo!=. 
>  
(236 real changes made)

.                 
.                 
.         ** Conversion of monetary variables to November 2009 chilean pesos
.         foreach y in ya1_sueldo ya1_negocio ya1_productos ya2_sueldo ya1 ya2 ylaborales ydeptes 
> yindeptes  {
  2.                 replace `y'=`y'*(ipc_nov2009/ipc)
  3.         }
(2037 real changes made)
(2542 real changes made)
(392 real changes made)
(287 real changes made)
(4590 real changes made)
(287 real changes made)
(4608 real changes made)
(2073 real changes made)
(2664 real changes made)

. 
.         
.         ** Wage employment, Self-employment and Employment (defined by income of those activitie
> s)
.         
.         gen dependiente = (ydeptes>0) if ind_c2010==1 & ydeptes!=. & yindeptes!=.
(6665 missing values generated)

.         gen independiente = (yindeptes>0) if ind_c2010==1 & ydeptes!=. & yindeptes!=.
(6665 missing values generated)

.         gen trabaja=(ylaborales>0) if ind_c2010==1 & ylaborales!=.
(6662 missing values generated)

.         
.                 
. 
.                 
.         ** Demographics
.         
.         ** Household size
.         quiet egen numper= count(orden) if ind_c2010==1, by(id)

.         
.         
.         ** Number of family cores in household
.         egen nucleos= max(a4) if ind_c2010==1, by(id)
(1337 missing values generated)

.         
.         ** Years of education
.         quiet gen esc= 0 if (b05t<=2 | b05t==7)

.         quiet replace esc=b05c if (b05t==3 | b05t==5) & (b05c!=88 & b05c!=99 & b05c!=.)

.         quiet replace esc=6 if b05t==4

.         quiet replace esc=8 if b05t==6

.         quiet replace esc=6 + b05c if (b05t==8 | b05t==12) & (b05c!=88 & b05c!=99 & b05c!=.)

.         quiet replace esc=12 if b05t==9 | b05t==11 | b05t==13 | b05t==15

.         quiet replace esc=8 + b05c if (b05t==10 | b05t==14) & (b05c!=88 & b05c!=99 & b05c!=.)

.         quiet replace esc=12 + b05c if (b05t==16 | b05t==17 | b05t==18) & (b05c!=88 & b05c!=99 &
>  b05c!=.)

.         quiet replace esc=17 + b05c if b05t==19 & (b05c!=88 & b05c!=99 & b05c!=.)

.         
.         
.         ** Educational level
.         gen basica= (b05t<=7) if (b05t!=88 & b05t!=99 & b05t!=.)
(1492 missing values generated)

.         gen media_inc= (b05t==8 | b05t==10 | b05t==12 | b05t==14) if (b05t!=88 & b05t!=99 & b05t
> !=.)
(1492 missing values generated)

.         gen media= (b05t==9 | b05t==11 | b05t==13 | b05t==15) if (b05t!=88 & b05t!=99 & b05t!=.)
(1492 missing values generated)

.         gen superior= (b05t>=16 & b05t<=19) if (b05t!=88 & b05t!=99 & b05t!=.)
(1492 missing values generated)

.                 tab media

      media |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |     12,299       81.79       81.79
          1 |      2,738       18.21      100.00
------------+-----------------------------------
      Total |     15,037      100.00

.                 tab sup

   superior |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |     14,460       96.16       96.16
          1 |        577        3.84      100.00
------------+-----------------------------------
      Total |     15,037      100.00

. 
.         
.         ** Age
.         gen edad =  a3 
(1337 missing values generated)

.         
.                 
.         ** Gender (1 male, 0 female)
.         generate sexo= a2
(1337 missing values generated)

.         recode sexo (2 = 0)
(sexo: 8611 changes made)

.         label define sexolbl 1 "Male" 0 "Female"

.         label values sexo sexolbl

.         label var sexo "Dummy Gender (1=Male)"

.         
.                 
. ** Information only for beneficiaries
. 
.          keep if orden==1       
(13349 observations deleted)

.          
. ************************************************************************************************
> ***********     
. *** 2. Etiquetando variables finales
. 
. label var dependiente "dummy para trabajador dependiente"

. label var independiente "dummy para trabajador independiente"

. label var trabaja "dummy para estar trabajando"

. label var ylaborales "ingresos laborales"

. label var ydeptes "ingresos provenientes de actividades dependientes"

. label var yindeptes "ingresos provenientes de actividades independientes"

. 
. 
. ************************************************************************************************
> ***********     
. *** 3. Guardando base
.         ** 3.1. ordeno y guardo base
.         keep id orden ind_c2010 mesp mesp_p C anymesp TT numper nucleos sexo edad esc basica med
> ia_inc media superior ydeptes yindeptes ylaborales independiente dependiente trabaja gradrate qu
> alityindex fichaps

.         
.         sort id orden

.         bysort id : keep if _n== 1
(0 observations deleted)

.         
.         drop orden

. 
.         sort id

.                         
.         save "${path_data}/variables2010", replace
file /Users/pabloguzmanpinto/Dropbox/PAME 3ie/Databases/variables2010.dta saved

.         
.                 
. log close
      name:  <unnamed>
       log:  /Users/pabloguzmanpinto/Dropbox/PAME 3ie/Do-files/Logs/4_Prepare variables 2010.log
  log type:  text
 closed on:  14 Dec 2015, 15:30:36
--------------------------------------------------------------------------------------------------
